﻿using MediatR;
using SheepCultivation.Common.Domain.Week2;
using SheepCultivation.Common.Domain.Week3;
using SheepCultivation.Common.Dto;
using SheepCultivation.Common.Infrastructure.Interface;
using SheepCultivation.Enum;
using Yitter.IdGenerator;
using static System.Runtime.InteropServices.JavaScript.JSType;

namespace SheepCultivation.API.AddliCation.SheepManagement
{
    public class EliminatesCreateHandler : IRequestHandler<EliminatesCreateCommon, Result>
    {
        #region 仓储
        /// <summary>
        /// 羊只死亡仓储
        /// </summary>
        public readonly IBaseRepository<DeathManagement> DeathManagementRepository;
        /// <summary>
        /// 羊只淘汰仓储
        /// </summary>
        public readonly IBaseRepository<Eliminates> EliminatesRepository;
        /// <summary>
        /// 羊只类型仓储
        /// </summary>
        public readonly IBaseRepository<SheepTypes> SheepTypesRepository;
        /// <summary>
        /// 日志
        /// </summary>
        public readonly ILogger<EliminatesCreateHandler> logger;
        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="deathManagementRepository">羊只死亡</param>
        /// <param name="eliminatesRepository">羊只淘汰</param>
        /// <param name="sheepTypesRepository">羊只类型</param>
        /// <param name="logger">日志</param>
        public EliminatesCreateHandler(IBaseRepository<DeathManagement> deathManagementRepository, IBaseRepository<Eliminates> eliminatesRepository, IBaseRepository<SheepTypes> sheepTypesRepository, ILogger<EliminatesCreateHandler> logger)
        {
            DeathManagementRepository = deathManagementRepository;
            EliminatesRepository = eliminatesRepository;
            SheepTypesRepository = sheepTypesRepository;
            this.logger = logger;
        }
        #endregion

        #region 羊只淘汰新增
        /// <summary>
        /// 羊只淘汰新增
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public async Task<Result> Handle(EliminatesCreateCommon request, CancellationToken cancellationToken)
        {
            var result = new Result() { Code = (int)CommonEnumCode.error };
            //异常处理
            try
            {
                logger.LogInformation("羊只淘汰新增成功!");
                var res = new Eliminates()
                {
                  BreedingSheepArchiveId=request.BreedingSheepArchiveId,
                    SheepvarietyId=request.SheepvarietyId,
                    BuildingId=request.BuildingId,
                    ColumnsId = request.ColumnsId,
                    EliminateState= request.EliminateState,
                    EliminatesRenamk= request.EliminatesRenamk,
                    EliminateMeasure= request.EliminateMeasure,
                    EliminatesTime= request.EliminatesTime,
                    SheepTypeId=request.SheepTypeId,
                    Numberes= request.Numberes,
                    SerialNumber= request.SerialNumber,
                    EliminatesRemark= request.EliminatesRemark,
                };
                //生成流水号ID
                request.SerialNumber = YitIdHelper.NextId().ToString();

                EliminatesRepository.Add(res);

                result.Code = (int)CommonEnumCode.success;
                result.Msg = "羊只淘汰新增成功";
            }
            catch (Exception ex)
            {
                logger.LogError("羊只淘汰新增失败!" + ex.Message);
                result.Msg = "羊只淘汰新增失败";
            }
            return result;
        }
        #endregion
    }
}
